package servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import constants.AttributeTypeConstants;
import myJDBC.lowLevel.AttributeTypes;
import myJDBC.lowLevel.Attributes;

@WebServlet("/SrvlCreateAttribute")
public class SrvlCreateAttribute extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		if ((request.getParameter("selAttrTypes") == null) || (request.getParameter("name") == "")) {
			response.sendRedirect("privateOffice");
		} else {
			int created;
			String name = request.getParameter("name");
			int id = Integer.parseInt(request.getParameter("selAttrTypes"));
			try {
				created=Attributes.createAttribute(name, id);
				if(id== AttributeTypeConstants.array){
					request.setAttribute("attrId", created);
					request.getRequestDispatcher("createListValues").forward(request, response);
				}else{					
					response.sendRedirect("privateOffice");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
